/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.taskplugins  
 */
package com.apache.bid.service.taskplugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidItemPrice;
import com.apache.bid.manager.BidItemPriceManager;
import com.apache.tools.DateUtils;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  计算标的状态定时器
 * @author Hou Dayu 创建时间：2016-3-29  
 * 
 */
public class ComputeItemStatusTask implements PluginConnector, TimeTaskService {

	private transient static boolean ComputeItemStatus = true;
	private Logger log = Logger.getLogger(getClass());
	private BidItemPriceManager bidItemPriceManager;

	//private ProxyManager wbdProxyCacheManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		try {
			if (ComputeItemStatus) {
				ComputeItemStatus = false;
				ParamsVo<BidItemPrice> vo = new ParamsVo<BidItemPrice>();
				BidItemPrice bip = new BidItemPrice();
				vo.setObj(bip);
				List<BidItemPrice> list = bidItemPriceManager.getList(vo);
				if (!ToolsUtil.isEmpty(list)) {
					BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
					for (BidItemPrice price : list) {
						try {
							//							String itemKey = cache.item_prefix + price.getItemId();
							//							BidItemCacheVo itemVo = (BidItemCacheVo) cache.getCacheCloneByKey(itemKey);
							String bidTime = DateUtils.Now.fmt_yyyyMMdd_HHmmssSSS();
							BidCommonUtil.getInstance().getStatus(price, bidTime);
						} catch (Exception e) {
							log.error("获取状态失败：itemId=" + price.getItemId() + ";msg=" + e.getMessage());
						}
					}
				}
			}
		} finally {
			ComputeItemStatus = true;
		}
		return entity;
	}

	public boolean run() {
		try {
			execute(new ParamsVo());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void setBidItemPriceManager(BidItemPriceManager bidItemPriceManager) {
		this.bidItemPriceManager = bidItemPriceManager;
	}

	//	public void setWbdProxyCacheManager(ProxyManager wbdProxyCacheManager) {
	//		this.wbdProxyCacheManager = wbdProxyCacheManager;
	//	}

}
